package mshop.com.schild_kotlin.fragments

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import com.orhanobut.logger.Logger
import kotlinx.android.synthetic.main.fragment_home.*
import mshop.com.schild_kotlin.base.BaseFragment
import mshop.com.schild_kotlin.entitys.HomeBean
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.adapters.HomeAdapter
import mshop.com.schild_kotlin.entitys.Issue
import mshop.com.schild_kotlin.entitys.ItemList
import mshop.com.schild_kotlin.mvp.contract.HomeContract
import mshop.com.schild_kotlin.mvp.presenter.HomePresenter
import java.util.regex.Pattern

/**
 * Created by mayi on 2018/5/23.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */

class HomeFragment : BaseFragment(), HomeContract.View {


    var homeAdapter: HomeAdapter? = null
    var homePresenter:HomePresenter?=null
    var data:String=""
    var mList=ArrayList<ItemList>()

    //静态方法
    companion object {
        fun getInstall(): HomeFragment {
            val home_fragment = HomeFragment()
            val bundle = Bundle()
            home_fragment.arguments = bundle
            return home_fragment
        }
    }


    override fun initView() {
        homeAdapter=HomeAdapter(mList)
        recycle_view.layoutManager = LinearLayoutManager(context)
        recycle_view.adapter=homeAdapter
        homeAdapter?.notifyDataSetChanged()

        homePresenter= HomePresenter(context,this)

        //滑动到底部加载更多
        recycle_view.addOnScrollListener(object:RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                var layoutManager:LinearLayoutManager=recyclerView?.layoutManager as LinearLayoutManager
                var lastPosition=layoutManager.findLastVisibleItemPosition()
                if(newState==RecyclerView.SCROLL_STATE_IDLE && lastPosition>=mList.size-3){
                    if(!TextUtils.isEmpty(data)){
                        homePresenter?.moreData(data)
                    }
                }
            }
        })
    }

    override fun getLayoutResources(): Int {
        return R.layout.fragment_home
    }

    override fun setData(bean: HomeBean) {
//        Logger.d("接收到HomeBean %s",bean)
        val regEx = "[^0-9]"
        val p = Pattern.compile(regEx)
        val m = p.matcher(bean?.nextPageUrl)
        data = m.replaceAll("").subSequence(1, m.replaceAll("").length - 1).toString()
        if (refreshLayout!!.isRefreshing) {
            refreshLayout?.isRefreshing = false
            if (mList.size > 0) {
                mList.clear()
            }
        }


        bean.issueList.flatMap { it.itemList }
                .filter { it.type.equals("video") }
                .forEach { mList.add(it) }
        homeAdapter?.notifyDataSetChanged()
    }

    //Called when a swipe gesture triggers a refresh.
    override fun onRefresh() {
            homePresenter?.start()
    }

    override fun getNeedRefresh(): Boolean {
        return true
    }


}